from datetime import datetime
from django.contrib.gis.db import models
from django.contrib.auth.models import User
from tagging.models import Tag
from common.models import NameSlugModel, DateAwareModel, MyImageModel, UserOwnedModel

class PlaceManager(models.GeoManager):
    def get_by_natural_key(self, name, slug):
        return self.get(name=name, slug=slug)
	
class Place(NameSlugModel, DateAwareModel):
    address1 = models.TextField(blank=True)
    address2 = models.TextField(blank=True)
    city = models.TextField(blank=True)
    state = models.TextField(blank=True)
    zip = models.TextField(blank=True)
    website = models.TextField(blank=True)
    geometry = models.PointField(srid=4326, blank=True)
    objects = PlaceManager()
    phone_number = models.CharField(max_length=30, blank=True)

    def natural_key(self):
        lat, lng = self.get_lat_lng()
        return {
                'name' : self.name, #.replace("'", " & # 8 s "), 
                'slug' : self.slug,
                'lat'  : lat,
                'lng'  : lng
        }

    def get_lat_lng(self):
        # assuming geometry has 'POINT (38.23323 28.238923)' format
        return str(self.geometry).lstrip('POINT (').rstrip(')').split()

    def __unicode__(self):
        return self.name

class Dish(NameSlugModel, DateAwareModel):
    # for lists only
    last_added_date_time = models.DateTimeField(blank=True, null=True)
    last_added_to = models.IntegerField(blank=True, null=True)
    # for orders only
    last_sorted_date_time = models.DateTimeField(blank=True, null=True)
    last_sorted_in = models.IntegerField(blank=True, null=True)
    # required because place is a geo field
    objects = models.GeoManager()
    place = models.ForeignKey(Place)
    tags = models.ManyToManyField(Tag, blank=True)

    # used by view to include thumbnail url in JSON serialization 
    def get_thumbnail_url(self):
        if self.dishimage_set.all():
            return self.dishimage_set.all()[0].thumbnail.url
        return ''

    # used by view to include display view url in JSON serialization 
    def medium_img_url(self):
        if self.dishimage_set.all():
            return self.dishimage_set.all()[0].mediumdisplay.url
        return ''

    # used because i'm not sure why sometimes apostrophes messed up JSON and sometimes don't...
    def get_place_name(self):
        return self.place.name

    def natural_key(self):
        return {
                'name' : self.name, #.replace("'", " & # 8 s "),
                'place_name' : self.place.name, #.replace("'", " & # 8 s "),
                'id' : self.id,
        }

    class Meta:
        verbose_name_plural = ('dishes')

    def __unicode__(self):
        return self.name

class PlaceImage(MyImageModel, UserOwnedModel):
    place = models.ForeignKey(Place)

class DishImage(MyImageModel, UserOwnedModel):
    dish = models.ForeignKey(Dish)

    def get_thumbnail_url(self):
        return self.thumbnail.url